package com.tind.wyf.baselib.adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

/**
 * 未完工
 * @param <T>
 */
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> {
    protected Context mContext;
    protected LayoutInflater mInflater;

    public static final int ITEM_HEADER = 0;
    public static final int ITEM_INFO = -1;
    public static final int ITEM_FOOT = -2;

    public static final int NEITHER = 0;
    public static final int ONLY_HEADER = 1;
    public static final int ONLY_FOOTER = 2;
    public static final int BOTH_HEADER_FOOTER = 3;

    protected final int BEHAVIOR_MODE;

    public List<T> mItems;

    public BaseRecyclerViewAdapter(Context context, int mode) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.BEHAVIOR_MODE = mode;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        int type = getItemViewType(position);
        switch (type) {
            case ITEM_HEADER:
                return null;
            case ITEM_FOOT:
                return null;
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder viewHolder, int position) {

    }

    @Override
    public int getItemCount() {
        if (BEHAVIOR_MODE == ONLY_FOOTER || BEHAVIOR_MODE == ONLY_HEADER) {
            return mItems.size() + 1;
        } else if (BEHAVIOR_MODE == BOTH_HEADER_FOOTER) {
            return mItems.size() + 2;
        } else return mItems.size();
    }

    public int getCount() {
        return mItems.size();
    }

    protected int getIndex(int position) {
        return BEHAVIOR_MODE == ONLY_HEADER || BEHAVIOR_MODE == BOTH_HEADER_FOOTER ? position - 1 : position;
    }

    public void setItems(List<T> data) {
        this.mItems = data;
        notifyDataSetChanged();
    }

    public void addDatas(List<T> data) {
        if (data != null) {
            this.mItems.addAll(data);
            notifyItemRangeChanged(this.mItems.size(), data.size());
        }
    }

    public void addItem(T item) {
        if (item != null) {
            this.mItems.add(item);
            notifyItemChanged(mItems.size());
        }
    }

    public void addItem(T item, int position) {
        if (item != null) {
            this.mItems.add(getIndex(position), item);
            notifyItemInserted(position);
        }
    }

    public List<?> getItems() {
        return mItems;
    }

    public void replaceItem(int position, T item) {
        if (item != null) {
            this.mItems.set(getIndex(position), item);
            notifyItemChanged(position);
        }
    }

    public void updateItem(int position) {
        if (getItemCount() > position) {
            notifyItemChanged(position);
        }
    }


    public final void removeItem(T item) {
        if (this.mItems.contains(item)) {
            int position = mItems.indexOf(item);
            this.mItems.remove(item);
            notifyItemRemoved(position);
        }
    }

    public final void removeItem(int position) {
        if (this.getItemCount() > position) {
            this.mItems.remove(getIndex(position));
            notifyItemRemoved(position);
        }
    }

    public void moveItem(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(mItems, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(mItems, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    public final T getItem(int position) {
        int p = getIndex(position);
        if (p < 0 || p >= mItems.size())
            return null;
        return mItems.get(getIndex(position));
    }

    public void cleanPosition(int position) {
        if (null != mItems && 0 != mItems.size()) {
            mItems.remove(position);
            notifyItemInserted(position);
        }
    }

    public void resetItem(List<T> items) {
        if (items != null) {
            cleanAll();
            addDatas(items);
        }
    }

    public void cleanAll() {
        this.mItems.clear();
        notifyDataSetChanged();
    }
}
